package org.kjk.employee;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class EmployeeController {
	@Resource(name = "employeeDao")
	public EmployeeDAOImpl dao;

	@RequestMapping(value = "/listProduct.do", method = RequestMethod.GET)
	public void addProductView(HttpServletResponse response) throws IOException {
		response.getWriter().println("Hello Get Method");
		System.out.println("get method call");
		for (EmployeeDTO dto : dao.selectAll()) {
			response.getWriter().println("dto : " + dto.getEmployeeId());
		}
	}

	@RequestMapping(value = "/employeeRedirect.do", method = RequestMethod.GET)
	public String addProduct(HttpServletResponse response) throws IOException {
		return "employee/employeeSearchResult.jsp";
	}

	@RequestMapping(value = "/employeeSearch.do", method = RequestMethod.POST)
	public ModelAndView employeeSearch(@RequestParam("type") String queryType,
			@RequestParam("field") String queryField,
			@RequestParam("invis") String invisible) {

		System.out.println("type : " + queryType);
		System.out.println("field : " + queryField);
		System.out.println("invis : " + invisible);
		String sql = null;
		String[] param = null;
		if (queryField == null || queryType == null || invisible == null) {
			return new ModelAndView("/WEB-INF/employee/employeeSearchResult.jsp");
		}
		if (queryField.length() != 0 && queryType.equals("1")) {
			sql = "select * from employees where LAST_NAME = ?";
			param = new String[] { queryField };
		} else if (queryField.length() != 0 && queryType.equals("2")) {
			sql = "select * from employees where EMPLOYEE_ID = ?";
			param = new String[] { queryField };
		} else {
			sql = "select * from employees";
		}

		List<EmployeeDTO> result = (List<EmployeeDTO>) dao.select(sql, param);
		return new ModelAndView("/WEB-INF/employee/employeeSearchResult.jsp",
				"employee", result);
	}

	@RequestMapping(value = "/searchJsonEmployee.do")
	public void employeeSearchJson(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String queryType = request.getParameter("type");
		String queryField = request.getParameter("field");

		String sql = null;
		String[] param = null;
		if (queryType == null) {
			sql = "select * from employees";
		} else if (queryType.equals("1")) {
			sql = "select * from employees where LAST_NAME = ?";
			param = new String[] { queryField };
		} else if (queryType.equals("2")) {
			sql = "select * from employees where EMPLOYEE_ID = ?";
			param = new String[] { queryField };
		} else {
			sql = "select * from employees";
		}

		JSONObject json = dao.selectJson(sql, param);
		PrintWriter out = response.getWriter();
		out.println(json);
	}
}
